package com.ehotting.edsta.csharpServer.controller;

import com.ehotting.eaf.core.domain.R;
import com.ehotting.eaf.core.exception.ServiceException;
import com.ehotting.eaf.core.utils.StringUtils;
import com.ehotting.eaf.core.web.controller.BaseController;
import com.ehotting.eaf.core.web.domain.AjaxResult;
import com.ehotting.edsta.common.log.annotation.Log;
import com.ehotting.edsta.common.log.enums.BusinessType;
import com.ehotting.eaf.core.domain.csharpServer.LicenseSignedDto;
import com.ehotting.eaf.core.domain.csharpServer.LicenseApplyVo;
import com.ehotting.edsta.csharpServer.service.IUserLicenseService;
import com.ehotting.eaf.core.domain.system.api.UserLicense;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;

/**
 * C端用户licenseController
 * 
 * @author edsta
 * @date 2023-03-24
 */
@Api(tags = "UserLicenseController", description = "license相关接口")
@RestController
@RequestMapping("/license")
public class UserLicenseController extends BaseController
{
    @Autowired
    private IUserLicenseService userLicenseService;


    /**
     * 获取C端用户license详细信息
     */
//    @ApiOperation("根据id获取license信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(userLicenseService.selectUserLicenseById(id));
    }

    /**
     * 新增C端用户license
     */
//    @ApiOperation("新增license信息")
    @Log(title = "C端用户license", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserLicense userLicense)
    {
        return toAjax(userLicenseService.insertUserLicense(userLicense));
    }

    /**
     * 用户申请License
     * @return
     */
    @Log(title = "license模块", businessType = BusinessType.INSERT)
    @ApiOperation("生成license")
    @PostMapping("/licenseApply")
    public R<String> licenseApply(@RequestBody LicenseApplyVo licenseApplyVo){
        String licenseType = licenseApplyVo.getLicenseType();
        if (StringUtils.isEmpty(licenseType)){
            throw new ServiceException("许可证类型不能为空，请检查");
        }
        try {
            userLicenseService.generateLicense(licenseApplyVo);
            return R.okMsg("申请成功，需联系业务人员审核通过");
        } catch (Exception e) {
            logger.error("licenseApply:",e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 校验license
     * @return
     */
    @Log(title = "license模块", businessType = BusinessType.VERIFY)
    @ApiOperation("校验license")
    @PostMapping("/licenseVerify")
    public R<LicenseSignedDto> licenseVerify(@RequestBody LicenseSignedDto licenseSignedDto){
        try {
            return userLicenseService.licenseVerify(licenseSignedDto);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    @Log(title = "license模块", businessType = BusinessType.VERIFY)
    @ApiOperation("校验是否申请过license")
    @GetMapping("/isApplied")
    public R<Boolean> isApplied(){
        return R.ok(userLicenseService.isApplied());
    }

    /**
     * @param licenseSignedDto
     * @return
     */
    @Log(title = "license模块",businessType = BusinessType.UPDATE)
    @PostMapping("/licenseExtend")
    public R<String> licenseExtend(@RequestBody LicenseSignedDto licenseSignedDto){
        userLicenseService.licenseExtend(licenseSignedDto);
        return R.okMsg("申请成功，需联系业务人员审核通过");
    }
}
